/*
*	与地图交互的代码写到这个js中，这样有换地图的可能性
*/
$(function(){
	//初始化地图
	$("#map_canvas").bind("initmap.gts",function(e,data){	
		var me =data.me,language=me.language||"cn";//me=gts
		function mapsLoaded() {
			//108.94609317993158 34.53743075304973
			//国家地理坐标——经纬度的起算点和基准点 http://baike.baidu.com/view/81304.htm
			//显示中国地图
			window.gm || (window.gm=google.maps);
			var latlng = new gm.LatLng(34.53743075304973,108.94609317993158);
			me.log("initial google map");
			me.map = new gm.Map(document.getElementById("map_canvas"),{zoom:4,keyboardShortcuts:false,center: latlng,mapTypeId: gm.MapTypeId.ROADMAP});
		}

		window.loadMaps=function () {
			me.log("loading google map");
			google.load("maps", "3", {"callback" : mapsLoaded,other_params: "sensor=true&language="+language});
			window.loadMaps=null;//不要占全局变量
		}

		function initLoader() {
			me.log("initial google map loader");
			var script = document.createElement("script");
			script.src = "https://www.google.com/jsapi?callback=loadMaps";
			script.type = "text/javascript";
			document.getElementsByTagName("head")[0].appendChild(script);
		}
		initLoader();
	});
	
	//根据车类型和角度获取车的图片地址
	function getVelSrc(type,Angle){
		var imgs=["vehicle0.png","vehicle0_45.png","vehicle45.png","vehicle45_90.png","vehicle90.png","vehicle90_135.png","vehicle135.png","vehicle135_180.png","vehicle180.png","vehicle180_225.png","vehicle225.png","vehicle225_270.png","vehicle270.png","vehicle270_315.png","vehicle315.png","vehicle315_350.png"],idx=0; 
		if (Angle > 350 || Angle < 10) idx=0;
		if (Angle >= 10 && Angle <= 35) idx=1;
		if (Angle > 35 && Angle < 55) idx=2;
		if (Angle >= 55 && Angle <= 80) idx=3;
		if (Angle > 80 && Angle < 100) idx=4;
		if (Angle >= 100 && Angle <= 125) idx=5;
		if (Angle > 125 && Angle < 145) idx=6;
		if (Angle >= 145 && Angle <= 170) idx=7;
		if (Angle > 170 && Angle < 190) idx=8;
		if (Angle >= 190 && Angle <= 215) idx=9;
		if (Angle > 215 && Angle < 235) idx=10;
		if (Angle >= 235 && Angle <= 260) idx=11;
		if (Angle > 260 && Angle < 280) idx=12;
		if (Angle >= 280 && Angle <= 305) idx=13;
		if (Angle > 305 && Angle < 325) idx=14;
		if (Angle >= 325 && Angle <= 350) idx=15;
		return "resources/images/veh/"+type+"/"+imgs[idx];
	}
	
	//绘制marker
	function mergeView(data){
		if((!data.me) ||(!data.me.velData) ||(!data.vel) ||(!data.me.velData[data.vel]) || (!data.me.map)) return ;
		var me = data.me,map = me.map,velData=me.velData,curData=velData[data.vel],gm= google.maps, 
			myLatlng=new gm.LatLng(parseFloat(curData.Latitude),parseFloat(curData.Longitude));
		if(!curData.marker){
			curData.marker = new gm.Marker({
				position: myLatlng,
				icon:getVelSrc(curData.VehicleType||"car",curData.Angle)
			});
			curData.marker.setMap(me.map);  
			curData.marker.SimID=curData.SimID;//记住SimID
			curData.infowindow = new google.maps.InfoWindow({
				content: "弹出窗口<input type='button' value='按钮' />"
			});
		}else{
			curData.marker.setPosition(myLatlng);
			curData.marker.setIcon(getVelSrc(curData.VehicleType||"car",curData.Angle));
		}
		google.maps.event.addListener(curData.marker, 'click', function() {
			me.velData[this.SimID].infowindow.open(me.map,this);
		});

	}
	
	//点击左边的车辆列表:绘制marker，并设置中心点
	$("#map_canvas").bind("focusvel.gts",function(e,data){
		if((!data.me) ||(!data.me.velData) ||(!data.vel) ||(!data.me.velData[data.vel])   ||(!data.me.velData[data.vel].Longitude) || (!data.me.map)) return ;
		var me = data.me,map = me.map,velData=me.velData,curData=velData[data.vel],gm= google.maps, 
			myLatlng=new gm.LatLng(parseFloat(curData.Latitude),parseFloat(curData.Longitude));
		mergeView(data);
		me.map.setCenter(myLatlng);
	});

	//收到车辆位置信息
	$("#map_canvas").bind("mergeview.gts",function(e,data){
		if((!data.me) ||(!data.me.velData) ||(!data.vel) ||(!data.me.velData[data.vel]) || (!data.me.map)) return ;
		var me = data.me,map = me.map,velData=me.velData,curData=velData[data.vel],gm= google.maps, 
			myLatlng=new gm.LatLng(parseFloat(curData.Latitude),parseFloat(curData.Longitude));
		mergeView(data);
		//将所有车辆位置显示在视口中
		//找到最大/最小经(Longtitude)纬(Latitude)度，设置地图的边界
		var minlo=180,minla=90,maxlo=-180,maxla=-90;
		for (var i in me.velData){
			var item = me.velData[i];
			if(item.offline!==false) continue;//跳过离线车辆
			maxla=Math.max(maxla,item.Latitude);
			maxlo=Math.max(maxlo,item.Longitude);
			minla=Math.min(minla,item.Latitude);
			minlo=Math.min(minlo,item.Longitude);
		}
		if( maxla!=-90 && maxlo!=-180 && minla!=90 &&  minlo!=180 && maxla>=minla &&  maxlo>=minlo){
			me.map.fitBounds(new gm.LatLngBounds(new gm.LatLng( minla,minlo),new gm.LatLng( maxla,maxlo)));
		}else{
			//me.map.setCenter(myLatlng);
			//me.map.setZoom(15);
		}
	});
});